﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Acsys.Entity;
using System.Collections;

namespace Acsys
{
    public partial class TermOfPaymentForm : Form, IChildForm
    {
        TermOfPayment m_top = new TermOfPayment();
        IMainForm m_mainForm;
        int sortColumn = 0;
        public TermOfPaymentForm(Form parent, string name)
        {
            InitializeComponent();
            listView1.ColumnClick += new ColumnClickEventHandler(listView1_ColumnClick);
            InitializeButtonClick();
            this.Name = name;
            this.MdiParent = parent;
            m_mainForm = (IMainForm)parent;
            loadRecords();
            //ClearForm();
            //setEditMode(EditMode.New);
            if (listView1.Items.Count > 0) { listView1.Focus(); listView1.Items[0].Selected = true; }
        }
        void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            if (e.Column != sortColumn)
            {
                sortColumn = e.Column;
                listView1.Sorting = System.Windows.Forms.SortOrder.Ascending;
            }
            else
            {
                if (listView1.Sorting == System.Windows.Forms.SortOrder.Ascending)
                    listView1.Sorting = System.Windows.Forms.SortOrder.Descending;
                else
                    listView1.Sorting = System.Windows.Forms.SortOrder.Ascending;
            }
            this.listView1.ListViewItemSorter = new ListViewItemComparerAscDesc(e.Column);
            listView1.Sort();
            sortColumn = e.Column;
        }
        private void InitializeButtonClick()
        {
            toolStripButtonSave.Click += new EventHandler(Save);
            toolStripButtonEdit.Click += new EventHandler(Edit);
            toolStripButtonDelete.Click += new EventHandler(Delete);
            toolStripButtonClear.Click += new EventHandler(Clear);
        }
        private void setEditMode(EditMode editmode)
        {
            toolStripButtonSave.Enabled = (editmode == EditMode.New || editmode == EditMode.Update) && m_mainForm.CurrentUser.FORM_TOP_SAVE;
            toolStripButtonEdit.Enabled = (editmode == EditMode.View) && m_mainForm.CurrentUser.FORM_TOP_SAVE;
            toolStripButtonDelete.Enabled = (editmode == EditMode.View) && m_mainForm.CurrentUser.FORM_TOP_DELETE;
            toolStripButtonClear.Enabled = m_mainForm.CurrentUser.FORM_TOP_SAVE;
            ReloadMainFormButton();
        }
        private void ReloadMainFormButton()
        {
            m_mainForm.EnableButtonSave(toolStripButtonSave.Enabled);
            m_mainForm.EnableButtonEdit(toolStripButtonEdit.Enabled);
            m_mainForm.EnableButtonDelete(toolStripButtonDelete.Enabled);
            m_mainForm.EnableButtonClear(m_mainForm.CurrentUser.FORM_CCY_SAVE);
        }
        private void loadRecords()
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                listView1.Items.Clear();
                IList records = RepositoryFactory.GetInstance().CodeNameRepository().GetAll(m_top);
                foreach (TermOfPayment d in records)
                {
                    ListViewItem item = listView1.Items.Add(d.CODE);
                    item.SubItems.Add(d.NAME);
                    item.SubItems.Add(d.DAY.ToString());
                    item.Tag = d;
                }
                this.Cursor = Cursors.Default;
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }
        public void ClearForm()
        {
            try
            {
                textBoxCode.Text = "";//"ACC" + (RepositoryFactory.GetInstance().UserRepository().GetMaximumID(m_top) + 1).ToString().PadLeft(3, '0');
                textBoxName.Text = "";
                numericUpDownDay.Value = 0m;
                m_top = new TermOfPayment();
                errorProvider1.Clear();
                
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        public void Save(object sender, EventArgs e)
        {
            try
            {
                if (Valid())
                {
                    this.Cursor = Cursors.WaitCursor;
                    UpdateEntity();
                    if (m_top.ID == 0)
                    {
                        RepositoryFactory.GetInstance().CodeNameRepository().Save(m_top);
                    }
                    else
                    {
                        RepositoryFactory.GetInstance().CodeNameRepository().Update(m_top);
                    }
                    MessageBox.Show("Record has been saved");
                    ClearForm();
                    loadRecords();
                    textBoxCode.Focus();
                    this.Cursor = Cursors.Default;
                }
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }
        private void UpdateEntity()
        {
            m_top.CODE = textBoxCode.Text.Trim();
            m_top.NAME = textBoxName.Text.Trim();
            m_top.DAY = Convert.ToInt16(numericUpDownDay.Value);
        }
        public bool Valid()
        {
            bool a = textBoxCode.Text == "";
            bool b = textBoxName.Text == "";

            if (a) errorProvider1.SetError(textBoxCode, "Code Can not Empty");
            if (b) errorProvider1.SetError(textBoxName, "Name Can not Empty");
          

            return !a && !b;
        }
        public void Edit(object sender, EventArgs e)
        {
            setEnableForm(true);
            setEditMode(EditMode.Update);
        }
        public void setEnableForm(bool enable)
        {
            textBoxCode.ReadOnly = !enable;
            textBoxName.ReadOnly = !enable;
            numericUpDownDay.Enabled = enable;
        }
        public void Delete(object obj, EventArgs e)
        {
            try
            {
                if (m_top.ID > 0)
                {
                    this.Cursor = Cursors.WaitCursor;
                    if (MessageBox.Show("Please confirm to delete", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.No) { this.Cursor = Cursors.Default; return; }
                    RepositoryFactory.GetInstance().CodeNameRepository().Delete(m_top);
                    removeRecord(m_top.ID);
                    ClearForm();
                    setEnableForm(true);
                    setEditMode(EditMode.New);
                    this.Cursor = Cursors.Default;
                }

            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }
        private void removeRecord(int id)
        {
            foreach (ListViewItem item in listView1.Items)
            {
                TermOfPayment dep = (TermOfPayment)item.Tag;
                if (dep.ID == id)
                {
                    listView1.Items.Remove(item);
                    break;
                }
            }
        }
        public void Clear(object sender, EventArgs e)
        {
            ClearForm();
            setEnableForm(true);
            setEditMode(EditMode.New);
            textBoxCode.Focus();
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0) return;
            ClearForm();
            m_top = (TermOfPayment)listView1.SelectedItems[0].Tag;
            loadData();
            setEnableForm(false);
            setEditMode(EditMode.View);
        }
        private void loadData()
        {
            textBoxCode.Text = m_top.CODE;
            textBoxName.Text = m_top.NAME;
            numericUpDownDay.Value = m_top.DAY;
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }

        private void TermOfPaymentForm_Activated(object sender, EventArgs e)
        {
            ReloadMainFormButton();
        }

        public void Refresh(object sender, EventArgs e)
        {
            loadRecords();
        }
    }
}
